const express = require('express');
const multer = require('multer');
const path = require('path');
const OSS = require('ali-oss');


// https://houndpos.oss-cn-hangzhou.aliyuncs.com/profile/banner.png

const client = new OSS({
  region: 'oss-cn-hangzhou',  
  accessKeyId: 'LTAIz9H8q2V1Eatn',
  accessKeySecret: 'ntO0DEIZiOAQRMgjWrklPJjwbsbtlj',
  bucket: 'houndpos',
});

async function uploadImage(req, res) {

  const uploadedImage = req.file;
  const fileExtension = path.extname(uploadedImage.originalname);
  const fileName = `${Date.now()}${fileExtension}`;

  try {
    const result = await client.put(fileName, uploadedImage.path);
    res.send({
      message: 'File uploaded successfully',
      url: result.url,
    });
  } catch (err) {
    console.error(err);
    res.status(500).send('Failed to upload file.');
  }

}

module.exports = uploadImage